HA$PBExportHeader$u_cst_mosac_cabe.sru
forward
global type u_cst_mosac_cabe from u_cst_manto_main
end type
end forward

global type u_cst_mosac_cabe from u_cst_manto_main
event type boolean ue_grabar_error ( )
end type
global u_cst_mosac_cabe u_cst_mosac_cabe

type variables
long il_codigo
string is_docpath
end variables

forward prototypes
public function integer of_adjuntar ()
public function integer of_grabarimagen (integer ai_codigo, string as_docname)
end prototypes

event type boolean ue_grabar_error();integer li_gere
long  ll_idosac
string ls_ruta,ls_user

ll_idosac = dw_1.object.data[1,1]
ls_ruta =  dw_1.object.Img_DocuTxt[1]	

if len(ls_ruta) > 0  then
	select idgere into :li_gere from muser where iduser = :gs_iduser;
	//	select * from MGERENCIA where idGere=:li_gere;
	ls_user = ''
	select iduser into :ls_user from MUSER where idCargo = 3 and idGere = :li_gere;

	if ls_user = '' or isnull(ls_user)  or len(ls_user) = 0 then
		f_msj('El usuario con cargo de Gerente de Linea no esta configurado para recibir el mensaje',1)
		//rollback;
		return false
	end if

	if sqlca.Usp_Add_OSAC(ll_idosac,ls_user,'N','E') <> 0 then 
		RollBack;
		Return  FALSE
	End if		
else
	if sqlca.Usp_Add_OSAC(ll_idosac,'hchavez','N','E') <> 0 then 
		
		f_msg(sqlca.sqlerrtext,1)
		RollBack;
		Return  FALSE
	End if		
end if		

return TRUE
end event

public function integer of_adjuntar ();string  	ls_docID, ls_docpath, ls_docname, ls_MessageTitle
integer 	i, li_count, li_rtn, li_filenum,li_len


li_rtn = GetFileOpenName("Seleccione el archivo",    ls_docpath, ls_docname, "DOC",    "Doc Files (*.DOC;*.XLS),*.DOC;*.XLS,",     "C:\", 16410)

if li_rtn = 0 then 
	dw_1.object.Img_DocuTxt[1] = ''
	return - 1	 
end if

dw_1.object.Img_DocuTxt[1] = 	ls_docpath 

return 1
end function

public function integer of_grabarimagen (integer ai_codigo, string as_docname);string  	ls_docID, ls_docpath, ls_docname, ls_MessageTitle
integer 	i, li_count, li_rtn, li_filenum,li_len
Blob		lblb_Doc,lbl_temp
long ll_long

isnull(lblb_Doc)
isnull(lbl_temp)

//as_DocName = dw_1.object.Img_DocuTxt[1] 
if len(as_DocName) = 0 or isnull(as_DocName) or as_DocName = '' then return - 1

li_filenum = FileOpen ( as_DocName, StreamMode!, Read! )

If li_filenum = -1 or IsNull ( li_filenum ) Then
	f_msj("Error opening file",1 )
	Return - 1
End If

Do While FileRead(li_FileNum,lbl_temp) > 0 
	lblb_Doc += lbl_temp 
Loop 

FileClose ( li_FileNum )

Select count(*) into :li_count from mosac where idosac = :ai_codigo;

If li_count > 0 then
	//If MessageBox ( ls_MessageTitle, "A document with this ID exists. Do you want to replace it ?", Question!, YesNo! ) = 1 Then
	UpdateBlob mosac set img_docu = :lblb_Doc where idosac = :ai_codigo ;
	If SQLCA.SQLCode <> 0 Then
		MessageBox ("SQLErrorText", SQLCA.SQLErrText )
		rollback ;
		Return  - 1
	End If
End If

return 1

end function

on u_cst_mosac_cabe.create
int iCurrent
call super::create
end on

on u_cst_mosac_cabe.destroy
call super::destroy
end on

event ue_ver_default;boolean lb_sw
//if ab_ver = true then lb_sw = false
//if ab_ver = false then lb_sw = true

dw_1.enabled= false

dw_1.visible = false
dw_2.visible = false
uo_1.visible = true
uo_toolbarstrip.visible = false
st_titulo.visible = true
st_det1.visible = true
uo_toolbarstrip.of_setvisible( 'Enviar', false)
uo_toolbarstrip.of_setvisible( 'Cancelar', false)
uo_toolbarstrip.of_setvisible( 'Editar', false)
uo_toolbarstrip.of_setvisible( 'Borrar', false)
uo_toolbarstrip.of_setenabled( 'Enviar', false)		
uo_toolbarstrip.of_setenabled( 'Cancelar', false)
end event

event ue_ver_object;boolean lb_sw
if ab_ver = true then lb_sw = false
if ab_ver = false then lb_sw = true

dw_1.enabled= ab_ver
dw_2.enabled = ab_ver

uo_toolbarstrip.visible = true
uo_toolbarstrip.of_setvisible( 'Enviar', ab_ver)
uo_toolbarstrip.of_setvisible( 'Cancelar', ab_ver)
//uo_toolbarstrip.of_setvisible( 'Editar', lb_sw)
uo_toolbarstrip.of_setvisible( 'Borrar', lb_sw)
uo_toolbarstrip.of_setenabled( 'Enviar', ab_ver)		
uo_toolbarstrip.of_setenabled( 'Cancelar', ab_ver)

dw_1.visible = ab_ver
dw_2.visible = lb_sw
//iuo_grillamain.event dynamic ue_dw_enabled(true,ll_Rows
end event

event ue_nuevo;string ls_telefono,ls_area,ls_gerencia
integer li_gerencia,li_Area
LONG ll_movimiento

if dw_1.modifiedcount( ) > 0  then
	if  f_msj("$$HEX1$$bf00$$ENDHEX$$Esta seguro que desea crear un nuevo ticket?",2) = 2 then return
end if

is_button = 'Nuevo'
dw_1.reset()
dw_1.insertrow(0)
dw_1.object.data[1,1] = of_getcodigo()
ll_movimiento = 0
//sqlca.Usp_GetIdTablaINT('ORDEN_OSAC',ll_movimiento)
//dw_1.object.idOrden[1] = ll_movimiento
dw_1.object.iduser[1] = gs_iduser
dw_1.setcolumn('tasunto'); dw_2.setfocus()

dw_1.GetChild('idcatesoli', dwc);dwc.reset();	
dw_1.GetChild('idsoli', dwc); dwc.reset();	

dw_1.object.fproceso[1] = today()

select top 1 ttelefono,idgere,idarea into :ls_telefono,:li_gerencia,:li_area from muser where iduser = :gs_iduser;

dw_1.object.tnrotelefono[1] =ls_telefono
dw_1.object.idarea[1] =li_area
dw_1.object.idgere[1] =li_gerencia


dw_1.Modify("t_adjuntar.visible = 0")
dw_1.Modify("img_docutxt.visible = 0")
dw_1.Modify("b_adjuntar.visible = 0")


//dw_1.setcolumn(2) ; dw_1.setfocus( )
//IF LEN(IS_TABLA) > 0  THEN 


end event

event ue_grabar_previo;call super::ue_grabar_previo;LONG ll_movimiento
ll_movimiento =0
dw_1.object.data[1,1] = of_getcodigo()
sqlca.Usp_GetIdTablaINT('ORDEN_OSAC',ll_movimiento)
dw_1.object.idOrden[1] = ll_movimiento
il_codigo = dw_1.object.idOrden[1]
end event

event ue_setdwc;dw_2.GetChild("idtiposoli", dwc);dwc.reset();	dwc.settransobject( sqlca);	dwc.retrieve( )
dw_2.GetChild("idcatesoli", dwc);dwc.reset();	dwc.settransobject( sqlca);	dwc.retrieve( )
dw_2.GetChild("idsoli", dwc);dwc.reset();	dwc.settransobject( sqlca);	dwc.retrieve( )
dw_2.GetChild("idarea", dwc);dwc.reset();	dwc.settransobject( sqlca);	dwc.retrieve( )
dw_2.GetChild("idgere", dwc);dwc.reset();	dwc.settransobject( sqlca);	dwc.retrieve( )
string ls_ruta
ls_ruta = dw_2.object.Img_DocuTxt[1]

if len(ls_ruta) >  0 then
	dw_2.Modify("t_adjuntar.visible = 1")
	dw_2.Modify("img_docutxt.visible = 1")
	dw_2.Modify("b_adjuntar.visible = 1")
else
	dw_2.Modify("t_adjuntar.visible = 0")
	dw_2.Modify("img_docutxt.visible = 0")
	dw_2.Modify("b_adjuntar.visible = 0")
end if
end event

event ue_grabar;integer li_return=1,li_fila,li_data,li_soli,li_adjuntar
string ls_empresa,ls_data ,ls_soli,ls_ruta
long ll_idosac ,ll_soli

if dw_1.accepttext( ) =  - 1  then return 0
If f_findrequired(dw_1) = -1 Then Return 0

li_data = dw_1.object.idtiposoli[1]
if isnull(li_data) or li_data = 0  then 
	f_msj('ingrese el tipo de solicitud',1);	return 0
end if
li_data = dw_1.object.idcatesoli[1]
if isnull(li_data) or  li_data = 0  then 
	f_msj('ingrese la categoria de la solicitud',1);	return 0
end if
li_data = dw_1.object.idsoli[1]
if isnull(li_data) or  li_data = 0  then 
	f_msj('ingrese la solicitud',1);	return 0
end if

ls_data = dw_1.object.tmsj[1]
if isnull(ls_data) or trim(ls_data) = '' then 
	f_msj('ingrese el mensaje',1);	return 0
end if
li_soli = dw_1.object.idsoli[1]


select tdesc,isnull(badjuntar,0) into :ls_soli, :li_adjuntar  from msolicitud where idsoli = :li_soli;
//if SQLCA.SQLCODE=-1 then
//	messagebox('ERROR SQL',SQLCA.SQLERRTEXT)
//end if

if li_adjuntar = 0 then
	 dw_1.object.Img_DocuTxt[1]=''
end if

//dw_employee.GetItemString(3, "emp_name")
dw_1.object.ttipo[1] = 'O'
dw_1.object.tasunto[1] = ls_soli

ls_ruta = dw_1.object.Img_DocuTxt[1]

//
EVENT UE_GRABAR_PREVIO()
ll_idosac = dw_1.object.data[1,1]
dw_1.object.fproceso[1] = today()
if dw_1.update(true,false) = 1 then
	if This.Event ue_Grabar_Error() then
		dw_2.reset(); dw_2.clear()
		dw_1.RowsCopy(dw_1.GetRow(),dw_1.GetRow(), primary!, dw_2, 1, Primary!)
		dw_1.resetupdate();	commit;
		
		if len(ls_ruta) > 0   then
			//of_grabarimagen(ll_idosac,ls_ruta)
			f_grabar_img(ll_idosac,ls_ruta)
			commit;
		end if
		
		iuo_grillamain.event dynamic ue_refrescar_row(dw_2,ii_row,is_button)
		f_msj('Generacion de Ticket satisfactorio N$$HEX1$$ba00$$ENDHEX$$: ' + string(il_codigo),4)
		iuo_grillamain.event ue_activa_opc( false)
		event ue_ver_object(false)
		event ue_settabtext( 'Consultar')
		return 1
	else
		rollback ; f_msj('Error',1);	return 0
	end if
	
else
	//dw_1.resetupdate()
	rollback ;	f_msj('Error',1) ;	return 0
end if
return 0

//if dw_1.update(true,false) = 1 then
//	//NO HUBO ERROR AL GRABAR
//	if This.Event ue_Grabar_Error(false) then
//		MessageBox('Informaci$$HEX1$$f300$$ENDHEX$$n','El Proceso de Grabaci$$HEX1$$f300$$ENDHEX$$n se Realizo Exitosamente',information!)
//		this.event post ue_Grabar_Pos()
//		return 1
//	end if
//	
//else
//	//ERROR EN GRABAR
//	this.Event ue_Grabar_Error(TRUE) 
//end if
//
return 0
end event

type st_msj from u_cst_manto_main`st_msj within u_cst_mosac_cabe
end type

type st_det2 from u_cst_manto_main`st_det2 within u_cst_mosac_cabe
end type

type st_det1 from u_cst_manto_main`st_det1 within u_cst_mosac_cabe
end type

type st_titulo from u_cst_manto_main`st_titulo within u_cst_mosac_cabe
end type

type dw_2 from u_cst_manto_main`dw_2 within u_cst_mosac_cabe
integer width = 3058
integer height = 2040
string dataobject = "d_mosac_free"
end type

type uo_toolbarstrip from u_cst_manto_main`uo_toolbarstrip within u_cst_mosac_cabe
integer width = 3090
end type

event uo_toolbarstrip::constructor;this.of_AddItem('Enviar', 'Insert!')
this.of_AddItem('Cancelar', 'Undo!')
//this.of_AddItem('Editar', '..\IMG\ICO\edit.ico')
//this.of_AddItem('Borrar', '..\IMG\ICO\Delete.ico')

//opciones en estado invisible
this.of_setvisible( 'Enviar', false)
this.of_setvisible( 'Cancelar', false)
//esto es para mostar los textos
this.of_DisplayText(true)


end event

event uo_toolbarstrip::ue_buttonclicked;string ls_columna,ls_tipo,ls_codigo
integer li_codigo
long ll_codigo
choose case upper(as_button)
	
	case 'EDITAR'
		
		   is_button  =	as_button
		   iuo_tabmantomain.event dynamic ue_setbutton(as_button)
		   iuo_tabmantomain.event	dynamic ue_set_ib_sw(true)
//		   is_idcodigo =dw_2.object.data[1,1]
		   iuo_grillamain.event dynamic ue_activa_opc( true)
		   event ue_settabtext ('Editar '+ is_entidad)
		   event ue_ver_object(true)
		   ls_columna = dw_1.describe('#'+string(1)+'.name')
		   ls_tipo = dw_1.describe(ls_columna+'.coltype')	
			if left(ls_tipo,4)='char' then
				 ls_codigo =dw_2.object.data[1,1]
				 of_retrieve( ls_codigo)	
			elseif ls_tipo='int' then
				 li_codigo =dw_2.object.data[1,1]
				 of_retrieve( li_codigo)	
			else
				ll_codigo =dw_2.object.data[1,1]
				 of_retrieve( ll_codigo)	
			end if		

	case 'ENVIAR'
		event  ue_grabar( )
	case 'CANCELAR'
		
		if	event ue_getdwmodified( ) > 0 then
			if f_msg('Cambio realizados ... $$HEX1$$bf00$$ENDHEX$$Esta seguro que desea cancelar ?',7) = 2 then return
	
		end if
		IF UPPER(is_button) = 'EDITAR' Then
			event ue_ver_object(false)
		elseif	UPPER(is_button) = 'NUEVO'  THEN
			event ue_ver_default()
			 event ue_settabtext ('Principal')
			//uo_powerdock.of_setdocumenttext( iuo_tab ,'Consultar / '+is_entidad)
			//dw_1.enabled= false
		end if	
		dw_1.reset()
		iuo_grillamain.event dynamic ue_activa_opc(false)  
		iuo_grillamain.event dynamic ue_bloquear_refrescar(false) 
		//	iuo_tab.event dynamic ue_enabled_object(true)
		//	case 'SALIR'
		//		uo_powerdock.of_closedocument(uo_powerdock.of_getactiveobject( ))
END CHOOSE
end event

type dw_1 from u_cst_manto_main`dw_1 within u_cst_mosac_cabe
string dataobject = "d_mosac_free"
end type

event dw_1::itemchanged;integer li_var,li_codigo,li_adjuntar
this.accepttext( )
choose case dwo.name
	case 'tmsj'	
		If Trim(data) = '' Or IsNull(data) Then
			f_msj('Digite el mensaje',1)
			Return 0
		End If
	case 'idtiposoli'
		setnull(li_var)
		dw_1.object.idcatesoli[1] = li_var
		dw_1.object.idsoli[1] = li_var
		dw_1.GetChild('idcatesoli', dwc);dwc.reset();	
		dwc.settransobject( sqlca);	dwc.retrieve()
		dwc.setfilter('string(idtiposoli) = "'+string(data)+'"');dwc.filter();dwc.setsort( '#1 A');dwc.sort()
	case 'idcatesoli'
		setnull(li_var)
		dw_1.object.idsoli[1] = li_var
		dw_1.GetChild('idsoli', dwc);dwc.reset();	
		dwc.settransobject( sqlca);	dwc.retrieve()
		dwc.setfilter('string(idcatesoli) = "'+string(data)+'"');dwc.filter();dwc.setsort( '#1 A');dwc.sort()
	case 'idsoli'	
		li_codigo = integer (data)
		select isnull(badjuntar,0) into :li_adjuntar from msolicitud with (nolock) where idsoli = :li_codigo;
		dw_1.object.img_docutxt[1] = ''
		if li_adjuntar =  1 then
			dw_1.Modify("t_adjuntar.visible = 1")
			dw_1.Modify("img_docutxt.visible = 1")
			dw_1.Modify("b_adjuntar.visible = 1")
		else
			dw_1.object.img_docutxt[1] = ''
			dw_1.Modify("t_adjuntar.visible = 0")
			dw_1.Modify("img_docutxt.visible = 0")
			dw_1.Modify("b_adjuntar.visible = 0")
		end if
end choose

end event

event dw_1::itemerror;Choose Case dwo.name
	case 'tmsj'
		MessageBox('Advertencia','ingrese el mensaje',stopsign!)
		
End Choose

Return 1
end event

event dw_1::rowfocuschanged;dwitemstatus ldwitemstatus

if currentrow < 1 then return

//if ib_lista or ib_tablist then return

ldwitemstatus=this.getitemstatus(this.rowcount(),0,primary!)

if ldwitemstatus=new! or ldwitemstatus=newmodified! then
	this.scrolltorow(this.rowcount())
	return
end if

if ii_currentrow=0 then ii_currentrow=currentrow
//this.selectrow(ii_currentrow,FALSE)
//this.selectrow(currentrow,TRUE)
this.setrow(currentrow)
ii_currentrow=currentrow

end event

event dw_1::itemfocuschanged;call super::itemfocuschanged;//string ls_data
//Choose Case dwo.name
//	Case 'tnrotelefono'
//		
//		ls_data = This.GetItemString(row,'tmsj')
//		If IsNull(ls_data) or trim(ls_data)='' Then
//			MessageBox('Advertencia','Error, Ingrese el mensaje',StopSign!)
//			This.SetColumn('tmsj')
//		End If
//end choose
end event

event dw_1::buttonclicking;call super::buttonclicking;
choose case dwo.name 
	case 'b_adjuntar'
			of_adjuntar( )
	
		
end choose
end event

event dw_1::dberror;call super::dberror;f_msg(sqlerrtext,1)
return 1 
end event

type uo_1 from u_cst_manto_main`uo_1 within u_cst_mosac_cabe
end type

